<template>
  <div>
    <!-- 面包屑导航 -->
    <Breadcrumb />
    <el-card>
      <el-row>
        <el-col :span="4">
          <el-button type="primary" @click="showAddDiglog">添加分类</el-button>
        </el-col>
      </el-row>
      <!-- 表格 -->
      <tree-table
        style="margin-top: 20px"
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="{ row }">
          <i
            class="el-icon-success"
            v-if="row.cat_deleted === false"
            style="color: lightgreen"
          ></i>
          <i class="el-icon-error" style="color: lightgreen" v-else></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="{ row }">
          <el-tag size="mini" v-if="row.cat_level === 0">一级</el-tag>
          <el-tag type="success" size="mini" v-else-if="row.cat_level === 1"
            >二级</el-tag
          >
          <el-tag type="warning" size="mini" v-else>三级</el-tag>
        </template>
        <!-- 操作 -->
        <template slot="opt" slot-scope="{ row }">

            <el-button
              type="primary"
              @click="showEditDiglog(row)"
              size="mini"
              icon="el-icon-edit"
              >编辑</el-button
            >
   
 
            <el-button
              type="warning"
              size="mini"
              icon="el-icon-delete"
              @click="deleteCate(row)"
              >删除</el-button
            >
        
        </template>
      </tree-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 3, 5, 7, 9]"
        :page-size="queryInfo.pagesize"
        layout="total,sizes, prev, pager, next,jumper"
        :total="total"
        background
        style="text-align: center; margin-top: 20px"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类的对话框 -->
    <el-dialog
      :title="isEdit ? '修改分类' : '添加分类'"
      :visible.sync="addEditDialogVisible"
      width="50%"
      @close="resetData"
    >
      <el-form
        :model="addCateForm"
        :rules="addCateFormRules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item v-show="!isEdit" label="父级分类">
          <el-cascader
            v-model="selectedkeys"
            :options="parentCateList"
            :props="cascaderProps"
            @change="handleChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addEditDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addOrEditCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Cate",
  data() {
    return {
      //查询条件
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5,
      },
      //商品分类的数据列表,默认为空
      catelist: [],
      //总数据条数
      total: 0,
      addEditDialogVisible: false,
      //添加分类的表单数据对象
      addCateForm: {
        //将要添加分类的名称
        cat_name: "",
        //父级分类的id
        cat_pid: 0,
        //分类的等级
        cat_level: 0,
      },
      //父级分类列表
      parentCateList: [],
      //添加分类选中的id
      selectedkeys: [],
      //级联选择器配置
      cascaderProps: {
        expandTrigger: "hover",
        value: "cat_id",
        label: "cat_name",
        children: "children",
      },
      //修改标识
      isEdit: false,
      //编辑id
      currentEditId: -1,
      //添加分类表单校验
      addCateFormRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
        ],
      },
      //为table指定列的定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name",
          align: "center",
          headerAlign: "center",
        },
        {
          label: "是否有效",
          type: "template",
          //表示当前一列使用模板名称
          template: "isok",
          align: "center",
          headerAlign: "center",
        },
        {
          label: "排序",
          type: "template",
          //表示当前一列使用模板名称
          template: "order",
          align: "center",
          headerAlign: "center",
        },
        {
          label: "操作",
          type: "template",
          //表示当前一列使用模板名称
          template: "opt",
          align: "center",
          headerAlign: "center",
        },
      ],
    };
  },
  created() {
    this.getCareList();
  },
  methods: {
    //获取商品分类数据
    async getCareList() {
      const res = await this.$http.getCateList(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error("获取商品分类失败");
      }
      this.catelist = res.data.result;
      this.total = res.data.total;
    },
    //页面展示数量改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCareList();
    },
    //页码改变
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCareList();
    },
    //点击添加分类
    showAddDiglog() {
      this.getParentCateList();
      this.addEditDialogVisible = true;
    },
    // 监听对话框关闭重置数据
    resetData() {
      this.selectedkeys = [];
      this.addCateForm = {
        //将要添加分类的名称
        cat_name: "",
        //父级分类的id
        cat_pid: 0,
        //分类的等级
        cat_level: 0,
      };
      this.currentEditId = -1;
      this.isEdit = false;
    },
    //获取父级分类的数据列表
    async getParentCateList() {
      const res = await this.$http.getParentCateList();
      if (res.meta.status !== 200) {
        return this.$message.error("获取父级数据列表失败");
      }
      this.parentCateList = res.data;
    },
    //选择变化
    handleChange() {
      if (this.selectedkeys.length > 0) {
        this.addCateForm.cat_pid =
          this.selectedkeys[this.selectedkeys.length - 1];
        this.addCateForm.cat_level = this.selectedkeys.length;
      } else {
        this.addCateForm.cat_level = 0;
        this.addCateForm.cat_pid = 0;
      }
    },
    //点击编辑
    showEditDiglog(row) {
      this.isEdit = true;
      this.addCateForm.cat_name = row.cat_name;
      this.currentEditId = row.cat_id;
      this.addEditDialogVisible = true;
    },
    //点击添加或修改分类确定按钮
    addOrEditCate() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (!valid) return;
        if (this.isEdit) {
          const res = await this.$http.editCate(this.currentEditId, {
            cat_name: this.addCateForm.cat_name,
          });
          if (res.meta.status !== 200) {
            return this.$message.error("修改分类失败");
          }
          this.getCareList();
          this.addEditDialogVisible = false;
          this.$message.success("修改分类成功");
        } else {
          const res = await this.$http.addCate(this.addCateForm);
          if (res.meta.status !== 201) {
            return this.$message.error("添加分类失败");
          }
          this.getCareList();
          this.addEditDialogVisible = false;
          this.$message.success("添加分类成功");
        }
      });
    },
    //删除分类
    deleteCate(row) {
      this.$confirm("此操作将永久删除该分类, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await this.$http.deleteCate(row.cat_id);
          if (res.meta.status !== 200) {
            this.$message.error("删除分类失败");
          }
          this.$message.success("删除分类成功");
          this.getCareList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
  },
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>
